var popup;
function getPopUp(vectorLayer, click){
    // create select feature control
	
    var selectCtrl = new OpenLayers.Control.SelectFeature(vectorLayer);
    
    // define "createPopup" function
    var bogusMarkup = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
    function createPopup(feature){
		click.deactivate();
		var mainid='Feature Info';
		var ftype;
		if (feature.attributes.MAINID) {
			mainid = feature.attributes.MAINID;
			ftype = 'MAIN'	
		}
		if (feature.attributes.NODE) {
			mainid = feature.attributes.NODE;
			ftype = 'MANHOLE';	
		}
		if (feature.attributes.NODEID) {
			mainid = feature.attributes.NODEID;
			ftype = 'MANHOLE';	
		}
		if (feature.attributes.pid) {
			mainid = feature.attributes.pid;
			ftype = 'PARCEL';	
		}
		 
		var html = ['<table style="font-size: 10px;border-bottom:black">'];
		var k;
		for (k in feature.attributes) {
			html.push('<tr><td style="text-align:right"><b>' + k + ':</b></td><td style="text-align:left">{' + k  + '}</td></tr>');
		}
		html.push('</table>');
		if (popup) {
			
			if (OpenLayers.Util.indexOf(vectorLayer.selectedFeatures, popup.feature) >
                -1) {
                    selectCtrl.unselect(popup.feature);
                }
			popup.destroy();
		};
		if (ftype=='PARCEL') {
			html.push('<a href="/static/windshield.htm?pid={pid}" target="_blank">Windshield Result</a>')
		}
		
		
        popup = new GeoExt.Popup({
            title: ftype + ':' + mainid,
            location: feature,
            width: 300,
			height: 400,
			items: [{xtype: 'tabpanel',
					title: 'InfoWindow',
					activeTab: 0,
					items: [{
						title: 'GIS Info',
						html: new Ext.Template(html).apply(feature.attributes)
					},
					{title: 'PACP',
					autoLoad:'/runs?query=' + mainid + '&format=html'}
					]}],
//            html: html,
            maximizable: true,
            collapsible: true,
			autoScroll: true
        });
		
		click.activate();
        // unselect feature when the popup
        // is closed
        popup.on({
            close: function(){
                if (OpenLayers.Util.indexOf(vectorLayer.selectedFeatures, this.feature) >
                -1) {
                    selectCtrl.unselect(this.feature);
                }
            }
        });
        popup.show();
    }
    
    // create popup on "featureselected"
    vectorLayer.events.on({
        featureselected: function(e){
            createPopup(e.feature);
        }
    });
	return selectCtrl;
}
